package onlinebanking.client.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConfirmTransactionDetailsPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel ConfirmTransactionLabel;
	private JLabel TransactiontypeLabel;

	private ClientPanel parent;
	private JLabel TransactionStatusLabel;
	private JPanel ConfirmPanel;
	private JLabel DestAccountLabel;
	private JLabel SourceAccountLabel;
	private JPanel TransferDetailsPanel;
	private JPanel DetailPanel;
	private JLabel AmountTransferLabel;
	private JLabel ProcessedOnLabel;
	private JLabel PACLabel;
	
	private JButton ConfirmButton;
	private JPasswordField ConfirmPACField;
	private JPasswordField PACField;
	private JLabel ConfrimPAC;
	private JLabel ExpirationLabel;
	private JLabel initatedbyLabel;
	
	public JLabel DestinationAccountData;
	public JLabel SourceAccountData;
	public JLabel AmountData;
	public JLabel ProcessedOnData;
	public JLabel ExpirationData;
	public JLabel TransactionOwnerData;
	public JLabel TransactionStatusData;
	public JLabel TransactionTypeData;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	private int transactionid;
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ConfirmTransactionDetailsPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public ConfirmTransactionDetailsPanel() {
		super();
		initGUI();
	}
	
	public ConfirmTransactionDetailsPanel(ClientPanel parent,int transactionid) {
		super();
		this.parent = parent;
		initGUI();
		this.transactionid = transactionid;
		sendCommand(transactionid);
	}
	
	private void initGUI() {
		try {
			{
				this.setLayout(null);
				this.setBounds(0, 0, 498, 444);
				this.setPreferredSize(new java.awt.Dimension(524, 380));
				{
					ConfirmTransactionLabel = new JLabel();
					this.add(ConfirmTransactionLabel);
					ConfirmTransactionLabel.setText("Confrim Transaction");
					ConfirmTransactionLabel.setBounds(6, 6, 193, 30);
					ConfirmTransactionLabel.setFont(new java.awt.Font("Arial",1,16));
				}
				{
					DetailPanel = new JPanel();
					this.add(DetailPanel);
					DetailPanel.setBounds(6, 48, 239, 205);
					DetailPanel.setLayout(null);
					DetailPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					{
						TransactiontypeLabel = new JLabel();
						DetailPanel.add(TransactiontypeLabel);
						TransactiontypeLabel.setText("Transaction Type :");
						TransactiontypeLabel.setBounds(6, 12, 124, 23);
					}
					{
						TransactionStatusLabel = new JLabel();
						DetailPanel.add(TransactionStatusLabel);
						TransactionStatusLabel.setText("Transaction Status:");
						TransactionStatusLabel.setBounds(8, 41, 124, 26);
					}
					{
						initatedbyLabel = new JLabel();
						DetailPanel.add(initatedbyLabel);
						initatedbyLabel.setText("Transaction Owner:");
						initatedbyLabel.setBounds(8, 73, 111, 25);
					}
					{
						ExpirationLabel = new JLabel();
						DetailPanel.add(ExpirationLabel);
						ExpirationLabel.setText("Expires on:");
						ExpirationLabel.setBounds(8, 104, 88, 20);
					}
					{
						ProcessedOnLabel = new JLabel();
						DetailPanel.add(ProcessedOnLabel);
						ProcessedOnLabel.setText("Processed on:");
						ProcessedOnLabel.setBounds(8, 130, 92, 22);
					}
					{
						TransactionTypeData = new JLabel();
						DetailPanel.add(TransactionTypeData);
						TransactionTypeData.setText("<undefined>");
						TransactionTypeData.setBounds(114, 15, 117, 19);
					}
					{
						TransactionStatusData = new JLabel();
						DetailPanel.add(TransactionStatusData);
						TransactionStatusData.setText("<undefined>");
						TransactionStatusData.setBounds(132, 46, 99, 19);
					}
					{
						TransactionOwnerData = new JLabel();
						DetailPanel.add(TransactionOwnerData);
						TransactionOwnerData.setText("<undefined>");
						TransactionOwnerData.setBounds(131, 77, 100, 21);
					}
					{
						ExpirationData = new JLabel();
						DetailPanel.add(ExpirationData);
						ExpirationData.setText("<undefined>");
						ExpirationData.setBounds(108, 104, 123, 20);
					}
					{
						ProcessedOnData = new JLabel();
						DetailPanel.add(ProcessedOnData);
						ProcessedOnData.setText("<undefined>");
						ProcessedOnData.setBounds(106, 130, 128, 22);
					}
				}
				{
					TransferDetailsPanel = new JPanel();
					this.add(TransferDetailsPanel);
					TransferDetailsPanel.setBounds(267, 48, 251, 205);
					TransferDetailsPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					TransferDetailsPanel.setLayout(null);
					{
						AmountTransferLabel = new JLabel();
						TransferDetailsPanel.add(AmountTransferLabel);
						AmountTransferLabel.setText("Ammout to Transfer");
						AmountTransferLabel.setBounds(7, 19, 130, 20);
					}
					{
						SourceAccountLabel = new JLabel();
						TransferDetailsPanel.add(SourceAccountLabel);
						SourceAccountLabel.setText("Source Account:");
						SourceAccountLabel.setBounds(8, 54, 138, 17);
					}
					{
						DestAccountLabel = new JLabel();
						TransferDetailsPanel.add(DestAccountLabel);
						DestAccountLabel.setText("Destination Account");
						DestAccountLabel.setBounds(8, 87, 138, 18);
					}
					{
						AmountData = new JLabel();
						TransferDetailsPanel.add(AmountData);
						AmountData.setText("<undefined>");
						AmountData.setBounds(132, 20, 111, 18);
					}
					{
						SourceAccountData = new JLabel();
						TransferDetailsPanel.add(SourceAccountData);
						SourceAccountData.setText("<undefined>");
						SourceAccountData.setBounds(133, 51, 111, 23);
					}
					{
						DestinationAccountData = new JLabel();
						TransferDetailsPanel.add(DestinationAccountData);
						DestinationAccountData.setText("<undefined>");
						DestinationAccountData.setBounds(136, 88, 110, 17);
					}

				}
				{
					ConfirmPanel = new JPanel();
					this.add(ConfirmPanel);
					ConfirmPanel.setBounds(6, 265, 502, 109);
					ConfirmPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					ConfirmPanel.setLayout(null);
					{
						PACLabel = new JLabel();
						ConfirmPanel.add(PACLabel);
						PACLabel.setText("Personal Authentication Code");
						PACLabel.setBounds(8, 8, 225, 27);
					}
					{
						ConfrimPAC = new JLabel();
						ConfirmPanel.add(ConfrimPAC);
						ConfrimPAC.setText("Confirm Personal Authentication Code:");
						ConfrimPAC.setBounds(8, 41, 225, 21);
					}
					{
						PACField = new JPasswordField();
						ConfirmPanel.add(PACField);
						PACField.setBounds(239, 7, 140, 28);
					}
					{
						ConfirmPACField = new JPasswordField();
						ConfirmPanel.add(ConfirmPACField);
						ConfirmPACField.setBounds(239, 37, 140, 28);
					}
					{
						ConfirmButton = new JButton();
						ConfirmPanel.add(ConfirmButton);
						ConfirmButton.setText("Confirm Transaction");
						ConfirmButton.setBounds(143, 71, 140, 28);
						ConfirmButton.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								ConfirmButtonActionPerformed(evt);
							}
						});
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void sendCommand(int transactionid){
		this.parent.getSimpleClient().viewPendingTransactionDetails(transactionid);
	}
	
	private void ConfirmButtonActionPerformed(ActionEvent evt) {
		
		String PAC = new String(PACField.getPassword());
		String ConfirmPAC = new String(ConfirmPACField.getPassword());
		
		if(!PAC.equals(ConfirmPAC) || PAC.equals("")){
			JOptionPane.showMessageDialog(null, "Authentication codes do not match.", 
					"Incorrect codes", JOptionPane.OK_OPTION);
			return;
		}
		
		
		int decision = JOptionPane.showConfirmDialog(this,
				"Are you sure you want confirm this transaction?",
				"Confirm Transaction", JOptionPane.YES_NO_OPTION,
				JOptionPane.WARNING_MESSAGE);
		if (decision == JOptionPane.YES_OPTION) {
			try{
			long longPAC = Long.parseLong(PAC);
			long longCPAC = Long.parseLong(ConfirmPAC);
			this.parent.getSimpleClient().confirmTransaction(this.transactionid, longPAC, longCPAC);
			this.parent.switchToConfirmTransaction();
			}catch(NumberFormatException e){
				JOptionPane.showMessageDialog(null, "Incorrect Personal Authentication Code format");
			}
		}
	}

}
